/**
 * @file pid_controller.h
 * @brief PID Controller for H743-V2 Flight Control System
 * @author Flight Control Team
 * @date 2025-10-31
 */

#ifndef __PID_CONTROLLER_H
#define __PID_CONTROLLER_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>

/**
 * @brief PID controller structure
 */
typedef struct {
    float kp;               // Proportional gain
    float ki;               // Integral gain
    float kd;               // Derivative gain
    float integral;         // Integral accumulator
    float prev_error;       // Previous error for derivative
    float output_min;       // Minimum output limit
    float output_max;       // Maximum output limit
    float integral_min;     // Minimum integral limit
    float integral_max;     // Maximum integral limit
    float dt;               // Time step
    bool enable_anti_windup; // Enable anti-windup
} PID_Controller_t;

/**
 * @brief Initialize PID controller
 * @param pid Pointer to PID controller
 * @param kp Proportional gain
 * @param ki Integral gain
 * @param kd Derivative gain
 * @param dt Time step
 */
void PID_Init(PID_Controller_t *pid, float kp, float ki, float kd, float dt);

/**
 * @brief Set PID gains
 * @param pid Pointer to PID controller
 * @param kp Proportional gain
 * @param ki Integral gain
 * @param kd Derivative gain
 */
void PID_SetGains(PID_Controller_t *pid, float kp, float ki, float kd);

/**
 * @brief Set output limits
 * @param pid Pointer to PID controller
 * @param min Minimum output
 * @param max Maximum output
 */
void PID_SetOutputLimits(PID_Controller_t *pid, float min, float max);

/**
 * @brief Set integral limits
 * @param pid Pointer to PID controller
 * @param min Minimum integral
 * @param max Maximum integral
 */
void PID_SetIntegralLimits(PID_Controller_t *pid, float min, float max);

/**
 * @brief Update PID controller
 * @param pid Pointer to PID controller
 * @param setpoint Desired value
 * @param measurement Current value
 * @return Control output
 */
float PID_Update(PID_Controller_t *pid, float setpoint, float measurement);

/**
 * @brief Reset PID controller
 * @param pid Pointer to PID controller
 */
void PID_Reset(PID_Controller_t *pid);

#ifdef __cplusplus
}
#endif

#endif /* __PID_CONTROLLER_H */

